package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;
	@Autowired
	private UserAccountService userAccountService;
	@Autowired
	private UserAccountMapper userAccountMapper;

	/**
	 * 根据订单编号查询充值信息
	 * @param orderNo
	 * @return
	 */
	@Override
	public RechargeInfo getRechargeInfo(String orderNo) {
		RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>()
				.eq(RechargeInfo::getOrderNo, orderNo));
		return rechargeInfo;
	}


	/**
	 * 微信支付成功后，处理充值业务
	 * @param orderNo
	 * @return
	 */
	@Override
	public void rechargePaySuccess(String orderNo) {
		//1.首先查询充值订单信息
		RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>()
				.eq(RechargeInfo::getOrderNo, orderNo));
		String rechargeStatus = rechargeInfo.getRechargeStatus();
		if (SystemConstant.ORDER_STATUS_UNPAID.equals(rechargeStatus)){
			//2.修改充值订单状态为已支付
			rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
			rechargeInfoMapper.updateById(rechargeInfo);

			//充值的金额
			BigDecimal rechargeAmount = rechargeInfo.getRechargeAmount();
			//3.修改用户账户余额
			int row = userAccountMapper.update(null, new LambdaUpdateWrapper<UserAccount>()
					.eq(UserAccount::getUserId, rechargeInfo.getUserId())
					.setSql("total_amount = total_amount +" + rechargeAmount)
					.setSql("available_amount = available_amount +" + rechargeAmount)
					.setSql("total_income_amount = total_income_amount +" + rechargeAmount)
			);
			if (!(row > 0)){
				throw new GuiguException(500, "充值失败！");
			}
			//4.新增账户变动日志
			userAccountService.saveUserAccountDetail(rechargeInfo.getUserId(), "充值", SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, rechargeAmount, orderNo);
		}
	}

	/**
	 * 提交充值记录
	 * @param rechargeInfoVo
	 * @return
	 */
	@Override
	public Map<String, String> submitRecharge(RechargeInfoVo rechargeInfoVo) {
		RechargeInfo rechargeInfo = new RechargeInfo();
		rechargeInfo.setUserId(AuthContextHolder.getUserId());
		//为充值记录生成订单编号  确保全局唯一且趋势递增 形式：CZ当天日期+雪花算法
		String orderNo = "CZ" + DateUtil.today().replaceAll("-", "") + IdUtil.getSnowflakeNextId();
		rechargeInfo.setOrderNo(orderNo);
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
		rechargeInfoMapper.insert(rechargeInfo);

		//TODO 基于RabbitMQ实现自动关闭
		return Map.of("orderNo", orderNo);
	}
}
